//
//  RunInfo.m
//  stock
//
//  Created by Lqq on 14-2-9.
//  Copyright (c) 2014年 Maxicn. All rights reserved.
//


#define ACCOUNT_KEY  @"ACCOUNT_KEY"
#define CURRENT_GYM_KEY   @"CURRENT_GYM_KEY"


#import "RunInfo.h"
#import "AppDelegate.h"
#import "BaseNavgationController.h"


@implementation RunInfo{
    
}

+ (RunInfo *)sharedInstance
{
    static dispatch_once_t t;
    static RunInfo *sharedInstance = nil;
    dispatch_once(&t, ^{
        sharedInstance = [[RunInfo alloc] init];
    });
    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self){
        _account = [[M_Account alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:ACCOUNT_KEY] error:nil];
        _currentGym = [[M_Gym alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:CURRENT_GYM_KEY] error:nil];

    }
    return self;
}
#pragma mark Setter/Getter
- (void)setAccount:(M_Account *)account
{
    _account = account;
    NSDictionary *accountDic =[account toDictionary];
    [[NSUserDefaults standardUserDefaults] setObject:accountDic forKey:ACCOUNT_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

- (void)setCurrentGym:(M_Gym *)currentGym
{
    _currentGym = currentGym;
    NSDictionary *currentGymDic =[currentGym toDictionary];
    [[NSUserDefaults standardUserDefaults] setObject:currentGymDic forKey:CURRENT_GYM_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isLogin
{
    return _account.token.length > 0;
}


- (void)loginOut
{
    self.account = nil;

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIStoryboard *accountStoryBoard = [UIStoryboard storyboardWithName:@"Account" bundle:nil];
    BaseNavgationController *loginNav = [accountStoryBoard instantiateInitialViewController];
    delegate.window.rootViewController = loginNav;
}

@end
